package com.ydd.app.manager.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ydd.app.action.CommonAct;
import com.ydd.app.bean.common.CustomerBean;
import com.ydd.app.bean.common.EmployeesBean;
import com.ydd.app.bean.input.CreteUserRequest;
import com.ydd.app.bean.input.CustomerAccountRequest;
import com.ydd.app.bean.output.BackstageLoginResponse;
import com.ydd.app.bean.output.CustomerLoginResponse;
import com.ydd.app.dao.AccountDao;
import com.ydd.app.manager.AccountMng;
import com.ydd.common.util.CommonUtil;

@Service
public class AccountMngImpl extends CommonAct implements AccountMng{
	
	@Autowired
	private AccountDao _accountDao;

	@Override
	public boolean isThisAccount(final String userId,final String token) {
		return false;
	}

	@Override
	public BackstageLoginResponse backstageLogin(final String account,final String password) {
		final BackstageLoginResponse response = new BackstageLoginResponse();
		final String token = UUID.randomUUID().toString();
		if( CommonUtil.isNotEmpty(token)) {
			final EmployeesBean employees = _accountDao.queryEmployeesByAccount(account);
			if( null != employees ) {
				if( CommonUtil.isNotEmpty(password) && password.equals(employees.getPrivateEmployeesPassowrd())) {
					if ( _accountDao.updateTokenForEmployees(account, token)) {
						response.setCode(CommonAct.STATIC_RET_OK);
						response.setEmployees(employees);
						response.setToken(token);
						return response;
					}
				}
				else {
					response.setCode(CommonAct.STATIC_RET_ERROR_3000);
					response.setMessage("账号密码或错误！");
					return response;
				}
			}
			else {
				response.setCode(CommonAct.STATIC_RET_ERROR_3001);
				response.setMessage("账号不存在！");
				return response;
			}
			
		}
		response.setCode(CommonAct.STATIC_RET_ERROR_1000);
		response.setMessage("系统异常！");
		return response;
	}

	@Override
	public CustomerLoginResponse customerLogin(final String account, final String password) {
		final CustomerLoginResponse response = new CustomerLoginResponse();
		final String token = UUID.randomUUID().toString();
		if( CommonUtil.isNotEmpty(token)) {
			final CustomerBean customer = _accountDao.queryCustomerByAccount(account);
			if( null != customer ) {
				if( CommonUtil.isNotEmpty(password) && password.equals(customer.getUserPAssword())) {
					if ( _accountDao.updateTokenForEmployees(account, token)) {
						response.setCode(CommonAct.STATIC_RET_OK);
						response.setCustomer(customer);
						response.setToken(token);
						return response;
					}
				}
				else {
					response.setCode(CommonAct.STATIC_RET_ERROR_3000);
					response.setMessage("账号密码或错误！");
					return response;
				}
			}
			else {
				response.setCode(CommonAct.STATIC_RET_ERROR_3001);
				response.setMessage("账号不存在！");
				return response;
			}
			
		}
		response.setCode(CommonAct.STATIC_RET_ERROR_1000);
		response.setMessage("系统异常！");
		return response;
	}

	@Override
	public boolean userAccount(final CustomerAccountRequest request) {
		final CustomerBean customer =_accountDao.queryCustomerByAccount(request.getUserID());
		if( null != customer ) {
			return false;
		}
		else {
			return _accountDao.userAccount(request);
		}
	}

	@Override
	public CustomerBean[] queryAllCustomers() {
		final List<CustomerBean> customers = _accountDao.queryAllCustomers();
		if( null != customers && customers.size() > 0 ) {
			return customers.toArray(new CustomerBean[0]);
		}
		else {
			return new CustomerBean[0];
		}
	}
	
	public boolean creteUser(final CreteUserRequest request){
		return _accountDao.creteUser(request);
	}

}
